export function groupBy(array, f) {
    let groups = {};
    array.forEach(function (o) {
        let group = JSON.stringify(f(o)); // ["fruits"]
        groups[group] = groups[group] || [];
        groups[group].push(o);
    });
    return Object.keys(groups).map(function (group) {
        return groups[group];
    });
}


export function nextTick(fn) {
    const callback = window.requestIdleCallback || window.setTimeout
    callback(() => {
        fn()
    })
}

export function debounce(func, delay) {
    let timer
    return function (sv, ...args) {
        if (timer) {
            window.clearTimeout(timer)
        }
        timer = window.setTimeout(() => {
            func.apply(sv, args)
        }, delay)
    }
}

export function toInt(obj) {
    if (!obj) {
        return 0;
    }
    let result = 0;
    try {
        result = parseInt(obj);
    } catch (e) {

    }
    return result;
}


export const setValue = (func) => {
    try {
        window["editor"].data;
        window.accessGet = function () {
            let data = func();
            if (typeof data == 'string') {
                data = JSON.parse(data);
            }
            return data;
        }
    } catch (e) {
        let data = func();
        if (typeof data == 'string') {
            data = JSON.parse(data);
        }
        if (data) {
            localStorage.setItem("$1", JSON.stringify(data));
        }

    }
}


export const getValue = (func) => {
    try {
        window["editor"].data;
        window.accessSet = function (data) {
            if (typeof data == 'string') {
                data = JSON.parse(data);
            }
            func(data);
        };
    } catch (e) {
        let data = localStorage.getItem("$1");
        if (data) {
            data = JSON.parse(data);
            func(data);
        }
    }
}